<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
      <el-form-item class="dapeng">
        <el-select v-model="value" placeholder="请选择" @change="getDataList">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="70"
      >
      </el-table-column>
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称"
        width="720"
      >
      </el-table-column>
      <el-table-column
        prop="category"
        header-align="center"
        align="center"
        label="种类"
        width="230"
      >
      </el-table-column>
      <el-table-column
        prop="date"
        header-align="center"
        align="center"
        label="时间"
      >
      </el-table-column>
      <el-table-column
        prop="publishCompany"
        header-align="center"
        align="center"
        label="发布单位"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            class="button"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            class="button"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            class="button"
            @click="checkHandle(scope.row.id)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog
      fullscreen
      title="详情信息"
      :visible.sync="dialogVisible"
      width="95%"
    >
    <div style="width:100%;height:126px" class="head"></div>
      <div class="Article">
        <div class="back-btn">
        <i class="el-icon-back"></i>
        <span>主页</span>
      </div>
        <div class="paper">
          <div style="height:50px"></div>
          <div class="title tac">
            {{ data.name }}
          </div>
          <div class="description">
            {{ data.date }}| {{ data.publishCompany }}
          </div>
          <div class="content" v-html="data.context"></div>
        </div>
        <div style="height:100px"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./zcdata-add-or-update";
export default {
  data() {
    return {
      dialogVisible: false, //弹窗显示判断
      dataForm: {
        key: ""
      },
      data: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      value: "1",
      options: [
        {
          value: "1",
          label: "惠农"
        },
        {
          value: "2",
          label: "生活社区"
        },
        {
          value: "3",
          label: "人才"
        },
        {
          value: "4",
          label: "生产园区"
        }
      ]
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList(value) {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/huinong/policiesRegulations/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: value
        })
      }).then(({ data }) => {
        console.log(this.dataForm.key);
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.dataList.context = "";
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    checkHandle(id) {
      this.$nextTick(() => {
        this.dataListLoading = true;
        (this.dialogVisible = true),
          this.$http({
            url: this.$http.adornUrl(`/huinong/policiesRegulations/info/${id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.data = data.policiesRegulationsEntity;
            }
          });
        this.dataListLoading = false;
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/huinong/policiesRegulations/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",

              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
<style scoped>
.head{
  background: url('../../assets/img/daohang.jpg') no-repeat ;
  width: 100%
}
.Article {
  width: 1200px;
  margin: 0 auto;
  background-color: rgba(241, 241, 241, 0.26);
}
.paper {
  width: 1000px;
  margin: 0 auto;
}

.title {
  font-size: 25px;
  color: #333;
  text-align: center;
  font-weight: 600;
}

.description {
  color: #888;
  font-size: 16px;
  line-height: 30px;
  text-align: center;
  margin-top: 10px;
}

.content {
  font-family: 宋体;
  font-size: 18px;
  line-height: 28px;
  color: #454545;
  margin-top: 20px;
  overflow: hidden;
  min-height: 450px;
  height: auto;
}

.button {
  background-color: #19678e;
  opacity: 0.9;
  width: 40px;
  height: 25px;
  color: white;
  padding-top: 6px;
}
</style>
